<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="params" :rules="paramsRules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="params.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="params.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="params.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <selectTree v-model="params.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="params.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="params.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="params.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <ImageUpload v-model="params.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="onSubit">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>

</template>

<script>
import { addEmployeeApi, changeEmployeeApi, getEmployeeDetailApi } from '@/api/employee'
import selectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'
export default {
  components: {
    selectTree,
    ImageUpload
  },
  data() {
    return {
      params: {
        username: '',
        workNumber: '',
        mobile: '',
        departmentId: null,
        formOfEmployment: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''// 头像
      },

      paramsRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['change', 'blur'] },
          { min: 1, max: 10, message: '1-10个字符', trigger: ['change', 'blur'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['change', 'blur'] },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: ['change', 'blur']
          }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: ['change', 'blur'] }
        ],
        departmentId: [
          { required: true, message: '部门不能为空', trigger: ['change', 'blur'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['change', 'blur'] }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: ['change', 'blur'] },
          { validator: (rules, value, callback) => {
            if (this.params.timeOfEntry) {
              if (new Date(this.params.timeOfEntry) > new Date(value)) {
                callback(new Error('转正日期不得小于入职时间'))
              } else {
                callback()
              }
            }
          }, trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  created() {
    this.$route.params.id && this.getEmployeeDetail()
  },
  methods: {
    onSubit() {
      this.$refs.userForm.validate(async(e) => {
        if (e) {
          if (this.$route.params.id) {
            await changeEmployeeApi(this.params)
            this.$message.success('修改成功')
          } else {
            await addEmployeeApi(this.params)
            this.$message.success('添加员工成功')
          }
          this.$router.back()
        }
      })
    },
    async getEmployeeDetail() {
      const res = await getEmployeeDetailApi(this.$route.params.id)
      this.params = res
    }

  }
}
</script>

  <style scoped lang="scss">
      .edit-form {
        background: #fff;
        padding: 20px;
        .inputW {
          width: 380px
        }
      }

  </style>
